﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using Com.CMS263.Util;

namespace Com.CMS263.DAO
{
    public  class BrandDAO:BaseService
    {
        #region 得到最大排序值
        /// <summary>
        /// 得到最大排序值
        /// </summary>
        /// <returns></returns>
        public int GetOrderMax()
        {
            string hql = "select max(SortOrder) from Brand";
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        public IList<Brand> FindAll()
        {
            string hql = "from Brand where IsShow='true' order by SortOrder desc";
            return FindList<Brand>(hql, null, null, null);
        }

        #region 读取品牌列表
        /// <summary>
        /// 读取品牌列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Brand> GetBrandList(int page, int pagesize)
        {
            try
            {
                string hql = "from Brand order by SortOrder ";
                return FindList<Brand>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region 品牌列表总数
        /// <summary>
        /// 品牌列表总数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            string hql = "select count(*) from Brand ";
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelBrand(string ids)
        {
            string hql = "delete from Brand where id in ("+ids+")";
            return DBHelper.ExecuteHQL(hql).ExecuteUpdate();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="orederid"></param>
        /// <returns></returns>
        public bool Delete(int orederid)
        {

            try
            {
                DBHelper.TransDeleteByID("Brand", orederid);
                return true;
            }
            catch { return false; }
        }
        #endregion

        #region 根据id查询实体
        /// <summary>
        /// 根据id查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Brand GetBrandById(int id)
        {
            try
            {
                string hql = "from Brand where Id=" + id;
                return DBHelper.ExecuteHQL(hql).List<Brand>().First();
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region 模糊查询list
        /// <summary>
        /// 模糊查询list
        /// </summary>
        /// <param name="like">查询条件</param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Brand> GetBrandByLike(string like, int page, int pagesize)
        {
            string hql = string.Empty;
            if (like == "显示" )
            {
                hql = "from Brand where BrandName like '%" + like + "%' or SiteUrl like '%" + like + "%' or SortOrder like '%" + like + "%'or IsShow='true'";
            }
            else if (like == "不显示")
            {
                hql = "from Brand where BrandName like '%" + like + "%' or SiteUrl like '%" + like + "%' or SortOrder like '%" + like + "%'or IsShow='false'";
            }
            else
            {
                hql = "from Brand where  BrandName like '%" + like + "%' or SiteUrl like '%" + like + "%' or SortOrder like '%" + like + "%'";
            }
            return FindList<Brand>(hql, null, (page - 1) * pagesize, pagesize);
        }
        #endregion

        #region 模糊查询条数
        /// <summary>
        /// 模糊查询条数
        /// </summary>
        /// <param name="like"></param>
        /// <returns></returns>
        public int GerBrandByLike(string like)
        {
            string hql = string.Empty;
            if (like == "显示")
            {
                hql = "select count(*) from Brand where BrandName like '%" + like + "%' or SiteUrl like '%" + like + "%' or SortOrder like '%" + like + "%'or IsShow='true'";
            }
            else if (like == "不显示")
            {
                hql = "select count(*) from Brand where BrandName like '%" + like + "%' or SiteUrl like '%" + like + "%' or SortOrder like '%" + like + "%'or IsShow='false'";
            }
            else
            {
                hql = "select count(*) from Brand where  BrandName like '%" + like + "%' or SiteUrl like '%" + like + "%' or SortOrder like '%" + like + "%'";
            }
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion
    }
}
